﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Kyoh.Lib.Plugin;

namespace Kyoh.App.Crawler
{
    partial class PluginManagerControl : UserControl
    {
        public PluginManagerControl()
        {
            InitializeComponent();
            pluginsDataGridView.AutoGenerateColumns = true;
            
            pluginUpButton.Click += UpClickEvent;
            pluginDownButton.Click += DownClickEvent;
            pluginInstallButton.Click += InstallClickEvent;
            pluginUninstallButton.Click += UninstallClickEvent;
            pluginUpdateButton.Click += UpdateClickEvent;
            pluginPreferenceButton.Click += PreferenceClickEvent;
            pluginsDataGridView.SelectionChanged += SelectionChangedEvent;

        }

        protected override Size DefaultSize { get { return new Size(200, 200); } }

        [Localizable(true)]
        [DefaultValue(null)]
        [Bindable(true)]
        [Category("Appearance")]
        public Image Image
        {
            get { return pluginsPictureBox.Image; }
            set { pluginsPictureBox.Image = value; }
        }

        public T[] GetSelectedItems<T>()
        {
            return
                pluginsDataGridView
                .SelectedRows
                .OfType<DataGridViewRow>()
                .Select(r => r.DataBoundItem)
                .OfType<T>()
                .ToArray();
        }

        [AttributeProvider(typeof(IListSource))]
        [RefreshProperties(RefreshProperties.Repaint)]
        [DefaultValue(null)]
        public object DataSource { get { return pluginsDataGridView.DataSource; } set { pluginsDataGridView.DataSource = value; } }
        public event EventHandler UpClick;
        public event EventHandler DownClick;
        public event EventHandler InstallClick;
        public event EventHandler UninstallClick;
        public event EventHandler UpdateClick;
        public event EventHandler PreferenceClick;
        public event EventHandler SelectionChanged;

        private void UpClickEvent(object sender, EventArgs args)
        {
            OnUpClick(args);
        }
        private void DownClickEvent(object sender, EventArgs args)
        {
            OnDownClick(args);
        }
        private void InstallClickEvent(object sender, EventArgs args)
        {
            OnInstallClick(args);
        }
        private void UninstallClickEvent(object sender, EventArgs args)
        {
            OnUninstallClick(args);
        }
        private void UpdateClickEvent(object sender, EventArgs args)
        {
            OnUpdateClick(args);
        }
        private void PreferenceClickEvent(object sender, EventArgs args)
        {
            OnPreferenceClick(args);
        }
        private void SelectionChangedEvent(object sender, EventArgs args)
        {
            OnSelectionChanged(args);
        }

        protected virtual void OnSelectionChanged(EventArgs args)
        {
            var handler = SelectionChanged;
            if (handler != null)
                handler(this, args);
        }
        protected virtual void OnUpClick(EventArgs args)
        {
            var handler = UpClick;
            if (handler != null)
                handler(this, args);
        }
        protected virtual void OnDownClick(EventArgs args)
        {
            var handler = DownClick;
            if (handler != null)
                handler(this, args);
        }
        protected virtual void OnInstallClick(EventArgs args)
        {
            var handler = InstallClick;
            if (handler != null)
                handler(this, args);
        }
        protected virtual void OnUninstallClick(EventArgs args)
        {
            var handler = UninstallClick;
            if (handler != null)
                handler(this, args);
        }
        protected virtual void OnUpdateClick(EventArgs args)
        {
            var handler = UpdateClick;
            if (handler != null)
                handler(this, args);
        }
        protected virtual void OnPreferenceClick(EventArgs args)
        {
            var handler = PreferenceClick;
            if (handler != null)
                handler(this, args);
        }

        [DefaultValue(true)]
        public bool PreferenceEnabled { get { return pluginPreferenceButton.Enabled; } set { pluginPreferenceButton.Enabled = value; } }

        [Browsable(false)]
        public object CurrentPreference
        {
            get
            {
                var plugin = CurrentPlugin;
                if (plugin == null)
                    return null;
                else
                    return plugin.Preferences;
            }
        }

        [Browsable(false)]
        public IPlugin CurrentPlugin
        {
            get
            {
                var plugins = this.GetSelectedItems<IPlugin>();
                if (plugins.Length == 0
                    || plugins.Length > 1)
                    return null;
                else
                    return plugins[0];
            }
        }
    }
}